package com.artisan.provider.controller;

import com.artisan.provider.contanent.Contanents;
import com.artisan.provider.covtransformation.CovCouponTransformation;
import com.artisan.provider.domain.ZgCoupon;
import com.artisan.provider.dto.ZgCouponDto;
import com.artisan.provider.service.ZgCouponService;
import com.artisan.provider.utils.Request;
import com.artisan.provider.vo.ZgCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ZgCouponController {

    @Autowired
    ZgCouponService zgCouponService;

    /***
     * 查询所有优惠券信息
     * @param zgCouponDto
     * @param page
     * @param limit
     * @return
     */
    @PostMapping(value = "/api/coupon/find/all/{page}/{limit}")
    public Request zgCouponFindAll(
                            @PathVariable(value = "page",required = false) int page,
                            @PathVariable(value = "limit",required = false)int limit,
                            @RequestBody(required = false) ZgCouponDto zgCouponDto
                           ){

        List<ZgCoupon> zgCoupons = zgCouponService.findAll(zgCouponDto,page,limit);
        List<ZgCouponVo> zgCouponVos = CovCouponTransformation.INSTANCE.zgCouponVos(zgCoupons);
        if (zgCoupons != null){
            return new Request(Contanents.DATA_SUCCESS,true,zgCouponVos);
        }else{
            return new Request(Contanents.DATA_FAILED,false,new ArrayList<>());
        }

    }

    /**
     * 数据的添加业务
     * @param zgCouponDto
     * @return
     */
    @PostMapping(value = "/api/coupon/add")
    public Request zgCouponAdd(@RequestBody ZgCouponDto zgCouponDto){
        ZgCoupon zgCoupon = CovCouponTransformation.INSTANCE.zgCoupon(zgCouponDto);
        if(zgCouponService.zgCouponAdd(zgCoupon) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

    /**
     * 数据的修改业务
     * @param zgCouponDto
     * @return
     */
    @PutMapping(value = "/api/coupon/update")
    public Request zgCouponUpdate(@RequestBody ZgCouponDto zgCouponDto){
        ZgCoupon zgCoupon = CovCouponTransformation.INSTANCE.zgCoupon(zgCouponDto);
        if(zgCouponService.zgCouponUpdate(zgCoupon) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

    /**
     * 数据的删除业务
     * @param id
     * @return
     */
    @DeleteMapping(value = "/api/coupon/delete/{id}")
    public Request zgCouponDelete(@PathVariable(value = "id")int id){

        if(zgCouponService.zgCouponDelete(id) == Contanents.DATA_ADD_REQUEST_ONE){
            return new Request(Contanents.DATA_SUCCESS,true,Contanents.DATA_ADD_REQUEST_ONE);
        }else{
            return new Request(Contanents.DATA_FAILED,false,Contanents.DATA_ADD_REQUEST_ZERO);
        }
    }

}
